﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Oracle.DataAccess.Client;
using System.IO;
using System.Text;

namespace Wicresoft.UnifyShow.Business
{
    public static class SyncHelper
    {

        public static void SyncAddDevice(DeviceSyncModel sync)
        {
            if (sync == null) return;
            string paramss = string.Empty;
            //string file = ConfigurationManager.AppSettings["SyncBPPMXFile"];
            string file = System.Web.Hosting.HostingEnvironment.MapPath("~/Sync/sync.txt");
            if (File.Exists(file)) File.Move(file, file + DateTime.Now.Year + DateTime.Now.Month + 
                DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".bck");
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("app_name;");
            sb.AppendLine("host_address='" + sync.Identify + "';");
            sb.AppendLine("app_name='" + sync.Name + "';");
            sb.AppendLine("END");
            File.WriteAllText(file, sb.ToString(),Encoding.Default);
            paramss += " -n @" + sync.GJ_IPPort2 + "#mc -d \"" + file + "\"";
            CMD.Run("SyncBPPMXAddDevice", paramss);
        }

        public static void SyncDeleteDevice(DeviceSyncModel sync)
        {
            if (sync == null) return;
            string paramss = string.Empty;
            paramss += " -n @" + sync.GJ_IPPort2 + "#mc -a app_name ";
            paramss += " -w \"host_address: == " + sync.Identify + ",app_name: == '" + sync.Name + "'\" -d -s DELETE";
            CMD.Run("SyncBPPMXDeleteDevice", paramss);
        }


        public static void SyncAddListBusinessSystemToSelf(IList<Dictionary<uint, string>> values)
        {
            string connectionStr = ConfigurationManager.ConnectionStrings["SelfServiceContext"].ConnectionString;
            using (OracleConnection con = new OracleConnection(connectionStr))
            {
                con.Open();
                OracleCommand command;
                foreach (var value in values)
                {
                    command = con.CreateCommand();
                    command.CommandType = System.Data.CommandType.Text;
                    command.CommandText = "INSERT INTO businesssystem (name,company,category,Isunified) VALUES(:name,:company,:category,0)";
                    command.BindByName = true;
                    OracleParameter name = new OracleParameter(":name", OracleDbType.NVarchar2, 100, value[536870925], System.Data.ParameterDirection.Input);
                    OracleParameter company = new OracleParameter(":company", OracleDbType.NVarchar2, 100, value[536890913], System.Data.ParameterDirection.Input);
                    OracleParameter category = new OracleParameter(":category", OracleDbType.NVarchar2, 100, value[536870915], System.Data.ParameterDirection.Input);
                    command.Parameters.Add(name);
                    command.Parameters.Add(company);
                    command.Parameters.Add(category);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }

        }

        public static void SyncDeleteBusinessSystemToSelf(Dictionary<uint, string> values)
        {
            string connectionStr = ConfigurationManager.ConnectionStrings["SelfServiceContext"].ConnectionString;
            using (OracleConnection con = new OracleConnection(connectionStr))
            {
                con.Open();
                OracleCommand command;
                command = con.CreateCommand();
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "DELETE FROM businesssystem b WHERE b.name=:name AND b.company=:company AND  b.category=:category";
                command.BindByName = true;
                OracleParameter name = new OracleParameter(":name", OracleDbType.NVarchar2, 100, values[536870925], System.Data.ParameterDirection.Input);
                OracleParameter company = new OracleParameter(":company", OracleDbType.NVarchar2, 100, values[536890913], System.Data.ParameterDirection.Input);
                OracleParameter category = new OracleParameter(":category", OracleDbType.NVarchar2, 100, values[536870915], System.Data.ParameterDirection.Input);
                command.Parameters.Add(name);
                command.Parameters.Add(company);
                command.Parameters.Add(category);
                command.ExecuteNonQuery();
                command.Dispose();
            }

        }


    }
}